/**
 * 
 */
package com.handinfo.uimenu.window;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameMIDlet;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.net.GameProtocol;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIPopmenu;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.Tools;

//#if edition == "JIUYOU"|| edition == "JIUYOU_1" || edition == "JIUYOU_2" || edition == "JIUYOU_3" || edition == "JIUYOU_4"|| edition == "JIUYOU_5" || edition == "JIUYOU_6"
//# import com.uc.gsdk.javame.api.UCGameSdk;
//# import com.uc.gsdk.javame.api.UCPayOrderInfo;
//# import com.uc.gsdk.javame.api.UCPayParam;
//# import com.uc.gsdk.javame.api.UCSdkEvent;
//# import com.uc.gsdk.javame.api.UCSdkListener;
//#endif

/**
 * @author venizeng
 * 
 */
public class UIChongzhi extends UIAbstractWindow {
	private final int STATE_SEL_CARD = 1;// 选择充值卡类型
	private final int STATE_SEL_YUNYINSHANG = 2;// 选择运行商
	private final int STATE_SEL_MONEY = 3;// 选择充值金额
	private final int STATE_RECORD = 4;// /元宝余额及充值记录
	private final int STATE_RECORD_DETAIL = 5;// /元宝余额及充值记录

	public UIChongzhi(int id) {
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
		//
		initDatas();
		initDis();
	}
	
	

	protected void registerTouch() {
		clearAllRects();
		registerNormalSoft();
	}



	/**
	 * 进入充值 2012-5-2 下午04:37:48 venizeng
	 */
	public static final void gotoChongzhi() {

		//#if edition == "JIUYOU"|| edition == "JIUYOU_1" || edition == "JIUYOU_2" || edition == "JIUYOU_3" || edition == "JIUYOU_4"|| edition == "JIUYOU_5" || edition == "JIUYOU_6"
		//# GameManager.getInstance().m_pause_run = true;
		//# UCPayParam payParam = new UCPayParam();
		//# StringBuffer buff = new StringBuffer();
		//# buff.append(GameManager.getInstance().m_role.m_gameID+",");
		//# buff.append(GameManager.getInstance().m_server_id+",");
		//# buff.append(""+1);
		//# payParam.customInfo = buff.toString();
		//# Tools.debugPrintln("customInfo="+payParam.customInfo);
		//# buff = null;
		//# UCGameSdk.defaultSdk().pay(new UCSdkListener() {
			
		//# 	public void onApiFinished(UCSdkEvent evt)
		//# 	{	
		//# 		if(evt.success){//充值成功
		//# 			UCPayOrderInfo order = (UCPayOrderInfo) evt.data;   
		//# 			int money = (int)order.amount;				
		//# 			String ordrID = order.orderId;
		//# 			String payWayname  = order.payWayName;			
		//# 			Tools.debugPrintln("money="+money);
		//# 			Tools.debugPrintln("ordrID="+ordrID);
		//# 			Tools.debugPrintln("payWayname="+payWayname);
		//# 			GameManager.getInstance().m_pause_run = false;
		//# 			GameManager.getInstance().m_game_message.sendChongzhi(payWayname, ""+money, ordrID, "");
		//# 			return;
		//# 		}
				//充值失败
		//# 		GameManager.getInstance().m_pause_run = false;
		//# 	}
		//# }, payParam);
	    //#else
		GameManager.getInstance().m_game_message.sendOpenChongzhi();
		// #endif
	}

	public static final void gotoRecord() {
		GameManager.getInstance().m_game_message.sendChongzhiRecord();
	}

	public static final void gotoYuanbaoConsumeRecord() {
		GameManager.getInstance().m_game_message.sendYuanbaoConsumeRecord();
	}

	private final int ID_YIDONG = 1;
	private final int ID_LIANTONG = 2;
	private final int ID_YIDONGZHIFUBAO = 3;
	

	private void initDatas() {
		m_ids_1 = new int[] { ID_YIDONG };
		m_items_1 = new String[m_ids_1.length];
		for (int i = 0; i < m_ids_1.length; i++) {
			switch (m_ids_1[i]) {
			case ID_YIDONG:
				m_items_1[i] = "移动充值卡";
				m_yunying_2 = STR_YIDONG;
				break;
			case ID_LIANTONG:
				m_items_1[i] = "联通充值卡";
				m_yunying_2 = STR_LIANTONG;
				break;
			case ID_YIDONGZHIFUBAO:
				m_items_1[i] = "移动支付宝";
				break;
			default:
				m_items_1[i] = "未知";
				break;
			}
		}
		m_des_2 = Tools.getStrArray("您需要知道的：`1.请再三确认您所充值的帐号、密码和金额`2.充值到账时间会有几分钟的延迟，请耐心等待`3.首次充值的玩家可以额外获得首充大礼包`4.如有任何充值问题，请拨打客服电话4000611230", UITools.W_CONTENT);
	}

	private void initDis() {
		int H = UITools.ENDY_CONTENT - UITools.INITY_CONTENT - H_DES_1 - (UITools.IH_RECTS << 1);
		int H_PRE = UITools.H_LABEL_STR + UITools.IH_RECTS;
		NUM_DIS_1 = (H / H_PRE);
		H_DRAG_1 = (NUM_DIS_1 - 1) * H_PRE + UITools.H_LABEL_STR;
		//
		H = UITools.H_RECT - (UITools.H_LABEL_STR + UITools.IH_RECTS) - (H_YUANBAO + UITools.IH_RECTS);
		NUM_RECORD = H / H_PRE_RECORD;

		H_RECORD = UITools.H_RECT - (UITools.IH_RECTS + UITools.H_LABEL_STR) - (UITools.IH_RECTS + H_YUANBAO);

	}

	public static void test() {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			dos.writeByte(1);
			dos.writeUTF("");
			dos.writeUTF("宁的卡萨的杀了孔得红啊控件的佳得乐哈卡好卡死了和德拉科调虎离山空间按喝多了空间啊是");
		} catch (IOException e) {
			e.printStackTrace();
		}

		DataInputStream dis = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()));
		try {
			recvMessage(0, dis);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void testRecord() {
		UIChongzhi chongzhi = (UIChongzhi) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_CHNGZHI);
		chongzhi.setWindowStatus(chongzhi.STATE_RECORD);
		chongzhi.m_chongzhi_records = chongzhi.getTestRecordStrs();
		chongzhi.m_consume_records = chongzhi.getTestConsumeStrs();
	}

	public static void recvMessage(int orderId, DataInput dis) throws IOException {
		switch (orderId) {
		case GameProtocol.CS_OPEN_CHONGZHI:// 开启
		{
			byte state = dis.readByte();
			String info = dis.readUTF();
			if (state == 0) {
				if (info.equals("")) {
					GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, info);
					return;
				}
			}
			UIChongzhi chongzhi = (UIChongzhi) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_CHNGZHI);
			if (chongzhi != null) {
				return;
			}
			chongzhi = (UIChongzhi) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_CHNGZHI);
			String des = dis.readUTF();
			chongzhi.m_des_1 = Tools.getStrArray(des, UITools.W_RECT_DRAG);
			chongzhi.setWindowStatus(chongzhi.STATE_SEL_CARD);
			UIManager.getInstance().addWindow(chongzhi);
		}
			break;
		case GameProtocol.CS_CHONGZHI_MIANER:// 获得面额
			break;
		case GameProtocol.CS_CHONGZHI_CONFIG: {
			byte state = dis.readByte();
			String info = dis.readUTF();
			if (!info.equals("")) {
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_D, info);
			}
			if (state == 1) {
				UIChongzhi chongzhi = (UIChongzhi) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_CHNGZHI);
				chongzhi.close();
			}
		}
			break;
		case GameProtocol.CS_QUERY_CHONGZHI: {
			byte state = dis.readByte();
			String info = dis.readUTF();
			if (state == 0) {
				if (!info.equals("")) {
					GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, info);
					return;
				}
			}
			UIChongzhi chongzhi = (UIChongzhi) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_CHNGZHI);
			chongzhi.setWindowStatus(chongzhi.STATE_RECORD);
			chongzhi.recvChongzhiRecord(dis);
			UIManager.getInstance().addWindow(chongzhi);
		}
			break;
		case GameProtocol.CS_QUERY_YUANBAO_CONSUME: {
			byte state = dis.readByte();
			String info = dis.readUTF();
			if (state == 0) {
				if (!info.equals("")) {
					GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, info);
					return;
				}
			}
			UIChongzhi chongzhi = (UIChongzhi) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_CHNGZHI);
			if (chongzhi == null) {
				chongzhi = (UIChongzhi) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_CHNGZHI);
				chongzhi.setWindowStatus(chongzhi.STATE_RECORD);
				chongzhi.recvConsumeRecord(dis);
				UIManager.getInstance().addWindow(chongzhi);
			} else {
				chongzhi.m_isLock = false;
				chongzhi.recvConsumeRecord(dis);
				chongzhi.setRecordState(chongzhi.RECORD_CONSUME);
			}
		}
			break;
		default:
			break;
		}
	}

	private void recvChongzhiRecord(DataInput dis) throws IOException {
		m_yuanbao_yue = dis.readLong();
		int num = dis.readInt();
		if (num == 0) {
			return;
		}
		m_chongzhi_records = new String[num][];
		for (int i = 0; i < num; i++) {
			int num_att = dis.readInt();
			m_chongzhi_records[i] = new String[num_att];
			for (int j = 0; j < num_att; j++) {
				m_chongzhi_records[i][j] = dis.readUTF();
			}
		}
	}

	private void recvConsumeRecord(DataInput dis) throws IOException {
		m_yuanbao_consum = dis.readLong();
		int num = dis.readInt();
		if (num == 0) {
			return;
		}
		m_consume_records = new String[num][];
		for (int i = 0; i < num; i++) {
			int num_att = dis.readInt();
			m_consume_records[i] = new String[num_att];
			for (int j = 0; j < num_att; j++) {
				m_consume_records[i][j] = dis.readUTF();
			}
		}
	}

	protected void actionOnStateChanged() {
		m_relY_des = 0;
		m_relY_record_detail = 0;
		switch (m_state) {
		case STATE_SEL_CARD:
			m_index_top_1 = 0;
			m_index_1 = 0;
			break;
		case STATE_SEL_MONEY:
		case STATE_SEL_YUNYINSHANG:
			m_index_2 = INDEX_SEL_MIANER_2;
			break;
		case STATE_RECORD:
			setRecordState(RECORD_CHONGZHI);
			break;
		default:
			break;
		}
	}

	protected void keyLogic() {
		switch (m_state) {
		case STATE_SEL_CARD:
			keylogicSelCard();
			break;
		case STATE_SEL_MONEY:
		case STATE_SEL_YUNYINSHANG:
			keyLogicSelMoney();
			break;
		case STATE_RECORD:
			keyLogicRecord();
			break;
		case STATE_RECORD_DETAIL:
			keyLogicRecordDetail();
		default:
			break;
		}
	}

	private void keyLogicRecordDetail() {
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			byte state = m_state_record ;
			setWindowStatus(STATE_RECORD);
			setRecordState(state);
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			if (m_relY_record_detail >= 0) {
				// m_relY_record_detail = 0;
				return;
			}
			m_relY_record_detail += AbstractGameScreen.FONT_H;
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			int h = getDetailHeight();
			int H = (UITools.H_RECT - (UITools.IH_RECT_CONTENT << 1)) - h;
			if (H > 0) {
				return;
			}
			if (m_relY_record_detail <= H) {
				// m_relY_record_detail = H ;
				return;
			}
			m_relY_record_detail -= AbstractGameScreen.FONT_H;
		}
	}

	private boolean m_isLock;

	private void keyLogicRecord() {
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			m_isLock = false;
			close();
			return;
		}
		if (m_isLock) {
			return;
		}
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT) || AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			setWindowStatus(STATE_RECORD_DETAIL);
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_D) || AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_B)) {
			if (m_state_record == RECORD_CHONGZHI) {
				String[][] infos = getRecords(RECORD_CONSUME);
				if (infos == null) {// 发送请求给消费记录
					GameManager.getInstance().m_game_message.sendYuanbaoConsumeRecord();
					m_isLock = true;
				} else {
					setRecordState(RECORD_CONSUME);
				}
			} else {
				setRecordState(RECORD_CHONGZHI);
			}
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			String[][] infos = getRecords();
			if (infos == null) {
				return;
			}
			if (m_index_record >= infos.length - 1) {
				return;
			}
			if (m_index_record - m_topIndex_record >= NUM_RECORD - 1) {
				m_topIndex_record++;
				m_index_record++;
			} else {
				m_index_record++;
			}
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			String[][] infos = getRecords();
			if (infos == null) {
				return;
			}
			if (m_index_record <= 0) {
				return;
			}
			if (m_index_record == m_topIndex_record) {
				if (m_topIndex_record == 0) {
					m_index_record = 0;
				} else {
					m_topIndex_record--;
					m_index_record--;
				}
			} else {
				m_index_record--;
			}
		}
	}

	private void keylogicSelCard() {
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT) || AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			int id = m_ids_1[m_index_1];
			switch (id) {
			case ID_YIDONG:
				setWindowStatus(STATE_SEL_MONEY);
				break;
			case ID_LIANTONG:
				setWindowStatus(STATE_SEL_MONEY);
				break;
			case ID_YIDONGZHIFUBAO:
				break;
			default:
				break;
			}
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			close();
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			if (m_index_1 == m_index_top_1) {
				if (m_index_top_1 != 0) {
					m_index_top_1--;
					m_index_1 = m_index_top_1;
				}
			} else {
				m_index_1--;
			}
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			int endIndex = getEndIndex();
			if (m_index_1 == endIndex) {
				if (endIndex < m_ids_1.length - 1) {
					m_index_top_1++;
					m_index_1++;
				}
			} else {
				m_index_1++;
			}
		}
	}

	private int getEndIndex() {
		int tempIndex = m_index_top_1 + NUM_DIS_1 - 1;
		return tempIndex >= m_ids_1.length - 1 ? m_ids_1.length - 1 : tempIndex;// 显示页最后一项的索引
	}

	private int getPlan() {
		if (m_index_1 == 0) {
			return 0;
		}
		return (m_index_1 + 1) * 100 / m_ids_1.length;
	}

	private final String STR_YIDONG = "移动";
	private final String STR_LIANTONG = "联通";
	private final String STR_DIANXIN = "电信";

	private void keyLogicSelMoney() {
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
			// if (m_yunying_2 == null) {
			// GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B,
			// "请输入运营商");
			// return;
			// }
			if (m_mianer_2 == null || m_mianer_2.equals("")) {
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "请输入面额");
				return;
			}
			if (m_cardId == null || m_cardId.equals("")) {
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "请输入卡号");
				return;
			}
			if (m_pass == null || m_pass.equals("")) {
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "请输入密码");
				return;
			}
			// 发送充值指令
			GameManager.getInstance().m_game_message.sendChongzhi(m_yunying_2, m_mianer_2, m_cardId, m_pass);
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			switch (m_index_2) {
			case INDEX_SEL_YUNYING_2:
				gotoSelYunying();
				break;
			case INDEX_SEL_MIANER_2:
				gotoSelMoney();
				break;
			case INDEX_SEL_COUNT_2:
			case INDEX_SEL_PASSWORD_2:
				gotoCountForm(this);
				break;
			}
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			setWindowStatus(STATE_SEL_CARD);
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			if (m_state == STATE_SEL_YUNYINSHANG) {
				return;
			}
			if (m_index_2 == INDEX_SEL_MIANER_2) {
				m_index_2 = INDEX_SEL_PASSWORD_2;
			} else {
				m_index_2--;
			}
		} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			if (m_state == STATE_SEL_YUNYINSHANG) {
				return;
			}
			if (m_index_2 == INDEX_SEL_PASSWORD_2) {
				m_index_2 = INDEX_SEL_MIANER_2;
			} else {
				m_index_2++;
			}
		}
	}

	private void gotoSelYunying() {
		final UIPopmenu popmenu = (UIPopmenu) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_POPMENU);
		final String[] temps = new String[] { STR_YIDONG, STR_LIANTONG, STR_DIANXIN };
		popmenu.setPopmenus(temps);
		popmenu.addListener(new UIListener() {

			public void actionPerformed() {
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT) || AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					m_yunying_2 = temps[popmenu.getCurrentIndex()];
					UIManager.getInstance().removeWindow(popmenu);
					setWindowStatus(STATE_SEL_MONEY);
				} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(popmenu);
				}
			}
		});
	}

	private String[] getMianer() {
		String[] temps = null;
		if (m_yunying_2 == STR_YIDONG) {
			temps = new String[] { "10元", "20元", "30元", "50元", "100元", "300元", "500元" };
		} else if (m_yunying_2 == STR_LIANTONG) {
			temps = new String[] { "20元", "30元", "50元", "100元", "300元", "500元" };
		} else if (m_yunying_2 == STR_DIANXIN) {
			temps = new String[] { "20元", "30元", "50元", "100元" };
		}
		return temps;
	}

	private void gotoSelMoney() {
		final String[] temps = getMianer();
		if (temps == null) {
			return;
		}
		final UIPopmenu popmenu = (UIPopmenu) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_POPMENU);
		popmenu.setPopmenus(temps);
		popmenu.addListener(new UIListener() {

			public void actionPerformed() {
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT) || AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					m_mianer_2 = temps[popmenu.getCurrentIndex()];
					UIManager.getInstance().removeWindow(popmenu);
				} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(popmenu);
				}
			}
		});
	}

	private void gotoCountForm(final UIChongzhi chongzhi) {
		Form myForm = new Form("输入卡号和密码");
		//
		final TextField cardId = new TextField("请输入卡号", m_cardId, 20, TextField.NUMERIC);
		final TextField pass = new TextField("请输入密码", m_pass, 20, TextField.NUMERIC);
		final StringItem warnInfoItem = new StringItem("", "");
		final Command ok = new Command("确定", Command.OK, 1);
		final Command cancel = new Command("返回", Command.CANCEL, 1);
		//
		myForm.append(cardId);
		myForm.append(pass);
		myForm.append(warnInfoItem);
		myForm.addCommand(ok);
		myForm.addCommand(cancel);
		//
		CommandListener listener = new CommandListener() {

			public void commandAction(Command c, Displayable d) {
				if (c == ok) {
					chongzhi.m_cardId = cardId.getString();
					chongzhi.m_pass = pass.getString();
					Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
				} else if (c == cancel) {
					Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
				}
			}
		};
		myForm.setCommandListener(listener);
		Display.getDisplay(GameMIDlet.s_instance).setCurrent(myForm);
	}

	protected void paintContent(Graphics g) {
		switch (m_state) {
		case STATE_SEL_CARD:
			paintSelCard(g);
			break;
		case STATE_SEL_YUNYINSHANG:
		case STATE_SEL_MONEY:
			paintSelYunyinAndMoney(g);
			break;
		case STATE_RECORD:
			paintRecord(g);
			break;
		case STATE_RECORD_DETAIL:
			paintRecordDetail(g);
			break;
		default:
			break;
		}
	}

	private int H_DES_1 = (AbstractGameScreen.FONT_H * 4) + (UITools.IH_RECT_CONTENT << 1);
	private int NUM_DIS_1;
	private int H_DRAG_1;
	private int m_index_top_1;
	private int m_index_1;
	private int[] m_ids_1;
	private String[] m_items_1;
	private String[] m_des_1;

	private void paintSelCard(Graphics g) {
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, "购买");
		// 绘制card选项
		int initY = m_y + UITools.INITY_CONTENT;
		int tempX = m_x + UITools.INITX_CONTENT;
		int tempY = initY;
		int H_PRE = UITools.H_LABEL_STR + UITools.IH_RECTS;
		for (int i = 0; i < NUM_DIS_1; i++) {
			int index = m_index_top_1 + i;
			if (index >= m_items_1.length) {
				break;
			}
			UITools.paintLabelStr(g, tempX, tempY, UITools.W_RECT_DRAG, UITools.H_LABEL_STR, m_items_1[index], index == m_index_1);
			tempY += H_PRE;
		}
		// 绘制下拉
		UITools.paintScrollBar(g, m_x + UITools.X_DRAG, initY, H_DRAG_1, getPlan());
		// 绘制描述
		tempY = m_y + UITools.ENDY_CONTENT - H_DES_1 - UITools.IH_RECTS;
		drawSts(g, tempX, tempY, UITools.W_RECT, H_DES_1, m_des_1);
	}

	private int m_index_2;
	private final int INDEX_SEL_YUNYING_2 = 1;
	private final int INDEX_SEL_MIANER_2 = 2;
	private final int INDEX_SEL_COUNT_2 = 3;
	private final int INDEX_SEL_PASSWORD_2 = 4;
	private int H_DES_2;
	private String[] m_des_2;
	private String 	m_yunying_2 = STR_YIDONG;
	private String m_mianer_2 = "";
	private String m_cardId = "";
	private String m_pass = "";

	private void paintSelYunyinAndMoney(Graphics g) {
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, "充值");
		int tempX = m_x + UITools.INITX_CONTENT;
		int tempY = m_y + UITools.INITY_CONTENT;
		final int endX = m_x + m_w - UITools.INITX_CONTENT;
		final int H_PRE = UITools.H_LABEL_STR + UITools.IH_RECTS;
		final int IW = 10;

		String temp = "";
		int tempW = UITools.W_RECT - AbstractGameScreen.s_font.stringWidth(temp) - IW;

		// g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		// g.drawString(temp, tempX, tempY, 0);
		// UITools.paintLabelStr(g, endX - tempW, tempY, tempW,
		// UITools.H_LABEL_STR, m_yunying_2, m_index_2 == INDEX_SEL_YUNYING_2);
		// tempY += H_PRE;

		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		temp = "充值面额:";
		g.drawString(temp, tempX, tempY, 0);
		tempW = UITools.W_RECT - AbstractGameScreen.s_font.stringWidth(temp) - IW;
		UITools.paintLabelStr(g, endX - tempW, tempY, tempW, UITools.H_LABEL_STR, m_mianer_2, m_index_2 == INDEX_SEL_MIANER_2);

		tempY += H_PRE;

		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		g.drawString("充值卡号:", tempX, tempY, 0);
		tempY += H_PRE;
		//
		tempW = UITools.W_RECT;
		UITools.paintInputRect(g, tempX, tempY, tempW, UITools.H_LABEL_STR, m_cardId, m_index_2 == INDEX_SEL_COUNT_2);
		tempY += H_PRE;
		//
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		g.drawString("充值密码:", tempX, tempY, 0);
		tempY += H_PRE;
		//
		tempW = UITools.W_RECT;
		UITools.paintInputRect(g, tempX, tempY, tempW, UITools.H_LABEL_STR, m_pass, m_index_2 == INDEX_SEL_PASSWORD_2);
		tempY += H_PRE;

		H_DES_2 = UITools.H_RECT - (5 * H_PRE);
		// 绘制描述
		tempY = m_y + UITools.ENDY_CONTENT - H_DES_2 - UITools.IH_RECTS;
		drawSts(g, tempX, tempY, UITools.W_RECT, H_DES_2, m_des_2);
	}

	private int m_relY_des;// 偏移值，每次修改状态时候需要将其置0

	private void drawSts(Graphics g, int x, int y, int w, int h, String[] temps) {
		UITools.paintContentRect(g, x, y, w, h);
		if (temps == null) {
			return;
		}
		x += UITools.IW_RECT_CONTENT;
		y += UITools.IH_RECT_CONTENT;
		w -= (UITools.IW_RECT_CONTENT << 1);
		h -= (UITools.IH_RECT_CONTENT << 1);
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		g.setClip(x, y, w, h);
		if (temps != null) {
			g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
			for (int i = 0; i < temps.length; i++) {
				if (temps[i] == null) {
					break;
				}
				g.drawString(temps[i], x, y + m_relY_des, 0);
				y += (AbstractGameScreen.FONT_H);
			}
		}
		int H = (h - (UITools.IH_RECT_CONTENT << 1)) - temps.length * AbstractGameScreen.FONT_H;
		if (H < 0) {
			m_relY_des--;
		}
		if (m_relY_des < H) {
			m_relY_des = 0;
		}
		g.setClip(clipX, clipY, clipW, clipH);
	}

	private int NUM_RECORD = 4;
	private int H_RECORD;
	private final int H_PRE_RECORD = (AbstractGameScreen.FONT_H << 1);
	private final int H_YUANBAO = (AbstractGameScreen.FONT_H) + (UITools.IH_RECT_CONTENT << 1);
	private int m_topIndex_record;
	private byte m_state_record;// 0 chongzhi,1 consume
	private int m_index_record;
	private String[][] m_chongzhi_records;
	private String[][] m_consume_records;
	private long m_yuanbao_yue;// 元宝余额
	private long m_yuanbao_consum;// 消费金额

	private final int W_RECORD_LABEL = 70;
	//
	private final byte RECORD_CHONGZHI = 0;
	private final byte RECORD_CONSUME = 1;

	private void setRecordState(byte type) {
		m_state_record = type;
		m_index_record = 0;
		m_topIndex_record = 0;
	}

	private String[][] getRecords() {
		return getRecords(m_state_record);
	}

	private String[][] getRecords(byte state) {
		return state == RECORD_CHONGZHI ? m_chongzhi_records : m_consume_records;
	}

	private void paintRecord(Graphics g) {
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, "记录");
		paintRecordArr(g);
		paintRecordLabels(g);
		paintItems(g);
		paintYuanbaos(g);
		paintDrag(g);
	}

	private void paintYuanbaos(Graphics g) {
		int tempX = m_x + UITools.INITX_CONTENT;
		int tempY = m_y + UITools.INITY_CONTENT + UITools.H_LABEL_STR + UITools.IH_RECTS + (H_RECORD + UITools.IH_RECTS);
		UITools.paintContentRect(g, tempX, tempY, UITools.W_RECT, H_YUANBAO);
		tempX += UITools.IW_RECT_CONTENT;
		tempY += UITools.IH_RECT_CONTENT;
		g.setColor(Tools.GREEN);
		if (m_state_record == RECORD_CONSUME) {
			g.drawString("消费总额:", tempX, tempY, 0);
		} else {
			g.drawString("元宝余额:", tempX, tempY, 0);
		}
		tempX = m_x + m_w - UITools.INITX_CONTENT - UITools.IW_RECT_CONTENT;
		tempY = tempY + H_YUANBAO - UITools.IH_RECT_CONTENT;
		if (m_state_record == RECORD_CONSUME) {
			UITools.paintMoneyRmbRB(g, tempX, tempY, m_yuanbao_consum, false);
		} else {
			UITools.paintMoneyRmbRB(g, tempX, tempY, m_yuanbao_yue, false);
		}
	}

	private final int X_LABEL_FIRST = ((UITools.W_BG - (2 * W_RECORD_LABEL + (2 - 1) * UITools.IW_RECT_CONTENT)) >> 1);// 居中显示
	private final int X_ARR_LEFT = X_LABEL_FIRST - (UIManager.IMAGE_LABEL_ARR.getWidth()) - 2;
	private final int X_ARR_RIGHT = UITools.W_BG - X_ARR_LEFT - (UIManager.IMAGE_LABEL_ARR.getWidth());

	private void paintRecordArr(Graphics g) {
		// 最后一个参数为true，因为可循环选择
		boolean isLight = true;
		boolean isLeft = true;
		UITools.paintLabelArr(g, m_x + X_ARR_LEFT, m_y + UITools.INITY_CONTENT, isLeft, isLight);
		UITools.paintLabelArr(g, m_x + X_ARR_RIGHT, m_y + UITools.INITY_CONTENT, !isLeft, isLight);
	}

	private void paintRecordLabels(Graphics g) {
		String[] m_labels = new String[] { "充值", "消费" };
		int tempX = m_x + X_LABEL_FIRST;
		int tempY = m_y + UITools.INITY_CONTENT;
		int w = W_RECORD_LABEL;
		for (int i = 0; i < m_labels.length; i++) {
			UITools.paintLabelStr(g, tempX, tempY, w, UITools.H_LABEL_STR, m_labels[i], m_state_record == i + RECORD_CHONGZHI);
			tempX += w + UITools.IW_RECTS;
		}
	}

	private void paintItems(Graphics g) {
		int tempX = m_x + UITools.INITX_CONTENT;
		int tempY = m_y + UITools.INITY_CONTENT + UITools.H_LABEL_STR + UITools.IH_RECTS;
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		g.setClip(tempX, tempY, UITools.W_RECT_DRAG, UITools.H_RECT - (UITools.H_LABEL_STR + UITools.IH_RECTS));
		UITools.paintContentRect(g, tempX, tempY, UITools.W_RECT_DRAG, H_RECORD);
		tempX += UITools.IW_RECT_CONTENT;
		tempY += UITools.IH_RECT_CONTENT;
		int w = UITools.W_RECT_DRAG - (UITools.IW_RECT_CONTENT << 1);
		int h = H_RECORD - (UITools.IW_RECT_CONTENT << 1);
		String[][] infos = getRecords();
		if (infos != null) {
			for (int i = 0; i < NUM_RECORD; i++) {
				int index = m_topIndex_record + i;
				if (index >= infos.length) {
					break;
				}
				paintRecordItem(g, infos, index, tempX, tempY, w, h);
				tempY += H_PRE_RECORD;
			}
		}
		g.setClip(clipX, clipY, clipW, clipH);
	}

	private void paintRecordItem(Graphics g, String[][] infos, int index, int x, int y, int w, int h) {
		if (infos == null) {
			return;
		}
		if (infos[index] == null) {
			return;
		}
		if (index == m_index_record) {
			g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_YESS));
		} else {
			g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		}
		int tempX = x;
		int tempY = y;
		switch (m_state_record) {
		case RECORD_CHONGZHI:
			g.setColor(COLOR_TITLE);
			g.drawString(infos[index][INDEX_CHONGZHI_TYPE_CARD], tempX, tempY, 0);
			tempX += w;
			if (infos[index][0].equals("0")) {// 失败状态
				g.setColor(Tools.RED);
				g.drawString("充值失败", tempX, tempY, Tools.RIGHT_TOP);// 失败提示
			} else {
				g.setColor(COLOR_CONTENT);
				g.drawString("充值 " + infos[index][INDEX_CHONGZHI_MIANER] + "元", tempX, tempY, Tools.RIGHT_TOP);// 金额
			}
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_CONTENT);
			g.drawString("价值 " + infos[index][INDEX_CHONGZHI_YUANBAO] + "元宝", tempX, tempY, Tools.RIGHT_TOP);// 金额
			break;
		case RECORD_CONSUME:
		default:
			// g.setColor(COLOR_CONTENT);
			// g.drawString(infos[index][INDEX_CONSUME_NAME] + "X" +
			// infos[index][INDEX_CONSUME_NUM], x, y, 0);

			g.setColor(COLOR_CONTENT);
			String temp = null ;
			if (infos[index][INDEX_CHONGZHI_STATE_CARD].equals("失败")) {
				g.drawString(infos[index][INDEX_CHONGZHI_TYPE_CARD], tempX, tempY, 0);
			}else{
				g.drawString(infos[index][INDEX_CONSUME_NAME], tempX, tempY, 0);
				tempX += AbstractGameScreen.s_font.stringWidth(infos[index][INDEX_CONSUME_NAME]) + 2;
				temp = "X";
				g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
				g.drawString(temp, tempX, tempY, 0);
				tempX += AbstractGameScreen.s_font.stringWidth(temp) + 1;
				g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_YESS));
				g.drawString(infos[index][INDEX_CONSUME_NUM], tempX, tempY, 0);
			}
			g.setColor(COLOR_CONTENT);
			temp = "消费:";
			tempX = x;
			tempY = y + AbstractGameScreen.FONT_H;
			g.drawString(temp, tempX, tempY, 0);
			tempX += AbstractGameScreen.s_font.stringWidth(temp);
			g.setColor(COLOR_TITLE);
			g.drawString(infos[index][INDEX_CONSUME_SUM], tempX, tempY, 0);
			tempX += AbstractGameScreen.s_font.stringWidth(infos[index][INDEX_CONSUME_SUM]);
			g.setColor(COLOR_CONTENT);
			g.drawString("元宝", tempX, tempY, 0);
			break;
		}
		if (m_index_record == index) {
			UITools.drawSelectBox(g, x, y, UITools.W_RECT_DRAG - (UITools.IH_RECT_CONTENT << 1), H_PRE_RECORD);
		}
	}

	private void paintDrag(Graphics g) {
		int plan = 0;
		String[][] info = getRecords();
		if (info != null && info.length != 0) {
			if (m_index_record != 0) {
				plan = 100 * (m_index_record + 1) / info.length;
			}
		}
		int tempY = m_y + UITools.INITY_CONTENT + UITools.H_LABEL_STR + UITools.IH_RECTS;
		UITools.paintScrollBar(g, m_x + UITools.X_DRAG, tempY, H_RECORD, plan);
	}

	private final int INDEX_CHONGZHI_TYPE_CARD = 0;
	private final int INDEX_CHONGZHI_MIANER = 1;// RMB
	private final int INDEX_CHONGZHI_CARDID = 2;
	private final int INDEX_CHONGZHI_TIME = 3;
	private final int INDEX_CHONGZHI_STATE_CARD = 4;
	private final int INDEX_CHONGZHI_DES_CARD = 5;
	private final int INDEX_CHONGZHI_ORDERID = 6;
	private final int INDEX_CHONGZHI_YUANBAO = 7;

	private final int INDEX_CONSUME_TYPE = 0;// 消费类型
	private final int INDEX_CONSUME_NAME = 1;
	private final int INDEX_CONSUME_NUM = 2;
	private final int INDEX_CONSUME_SUM = 3;
	private final int INDEX_CONSUME_TIME = 4;
	private final int INDEX_CONSUME_ORDERID = 5;

	private String[][] getTestRecordStrs() {
		String[][] infos = new String[12][];
		for (int i = 0; i < infos.length; i++) {
			infos[i] = new String[7];
			for (int j = 0; j < infos[i].length; j++) {
				infos[i][INDEX_CHONGZHI_TYPE_CARD] = "移动";
				infos[i][INDEX_CHONGZHI_MIANER] = "30";
				infos[i][INDEX_CHONGZHI_CARDID] = "82098";
				infos[i][INDEX_CHONGZHI_TIME] = "2012-08-21-08:00:00";
				infos[i][INDEX_CHONGZHI_STATE_CARD] = "1";
				infos[i][INDEX_CHONGZHI_DES_CARD] = "充值成功";
				infos[i][INDEX_CHONGZHI_ORDERID] = "9873892719318791";
			}
		}
		return infos;
	}

	private String[][] getTestConsumeStrs() {
		String[][] infos = new String[12][];
		for (int i = 0; i < infos.length; i++) {
			infos[i] = new String[6];
			for (int j = 0; j < infos[i].length; j++) {
				infos[i][INDEX_CONSUME_TYPE] = "3232";
				infos[i][INDEX_CONSUME_NAME] = "Item";
				infos[i][INDEX_CONSUME_NUM] = "2";
				infos[i][INDEX_CONSUME_SUM] = "100";
				infos[i][INDEX_CONSUME_TIME] = "2012-08-21-08:00:00";
				infos[i][INDEX_CONSUME_ORDERID] = "32132321";
			}
		}
		return infos;
	}

	private int m_relY_record_detail;

	private void paintRecordDetail(Graphics g) {

		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, "详情", false, true);
		int tempX = m_x + UITools.INITX_CONTENT;
		int tempY = m_x + UITools.INITY_CONTENT;
		UITools.paintContentRect(g, tempX, tempY, UITools.W_RECT_DRAG, UITools.H_RECT);

		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		g.setClip(tempX + UITools.IW_RECT_CONTENT, tempY + UITools.IH_RECT_CONTENT, UITools.W_RECT_DRAG - (UITools.IW_RECT_CONTENT << 1), UITools.H_RECT
				- (UITools.IH_RECT_CONTENT << 1));
		if (m_state_record == RECORD_CHONGZHI) {
			paintCZRecordDetail(g, m_relY_record_detail);
		} else {
			paintConsumeRecordDetail(g, m_relY_record_detail);
		}
		g.setClip(clipX, clipY, clipW, clipH);

		int plan = 0;
		int h = getDetailHeight();
		int H = (UITools.H_RECT - (UITools.IH_RECT_CONTENT << 1)) - h;
		if (H < 0) {//
			plan = 100 * m_relY_record_detail / H;
		}
		UITools.paintScrollBar(g, m_x + UITools.X_DRAG, tempY, UITools.H_RECT, plan);
	}

	private int getDetailHeight() {
		String[] infos = getDetail();
		if (infos == null) {
			return 0;
		}
		return infos.length * 2 * AbstractGameScreen.FONT_H;
	}

	private String[] getDetail() {
		String[][] infos = getRecords();
		if (infos == null) {
			return null;
		}
		String[] info = infos[m_index_record];
		if (info == null) {
			return null;
		}
		return info;
	}

	final int COLOR_TITLE = UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS);
	final int COLOR_CONTENT = Tools.GREEN;

	private void paintCZRecordDetail(Graphics g, int relY) {
		String[] info = getDetail();
		if (info == null) {
			return;
		}
		try {
			int tempX = m_x + UITools.INITX_CONTENT;
			int tempY = m_x + UITools.INITY_CONTENT;
			tempX += UITools.IW_RECT_CONTENT;
			tempY += UITools.IH_RECT_CONTENT + relY;
			g.setColor(COLOR_TITLE);
			g.drawString("订单号:", tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_CONTENT);
			g.drawString(info[INDEX_CHONGZHI_ORDERID], tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			if (info[INDEX_CHONGZHI_STATE_CARD].equals("失败")) {
				g.setColor(COLOR_TITLE);
				g.drawString("错误原因:", tempX, tempY, 0);
				tempY += AbstractGameScreen.FONT_H;
				g.setColor(COLOR_CONTENT);
				g.drawString(info[INDEX_CHONGZHI_DES_CARD], tempX, tempY, 0);
			}
			g.setColor(COLOR_TITLE);
			g.drawString("充值渠道商:", tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_CONTENT);
			g.drawString(info[INDEX_CHONGZHI_TYPE_CARD], tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_TITLE);
			g.drawString("充值面额:", tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_CONTENT);
			g.drawString(info[INDEX_CHONGZHI_MIANER] + "元", tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_TITLE);
			g.drawString("充值卡序列号:", tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_CONTENT);
			g.drawString(info[INDEX_CHONGZHI_CARDID], tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_TITLE);
			g.drawString("充值时间:", tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_CONTENT);
			g.drawString(info[INDEX_CHONGZHI_TIME], tempX, tempY, 0);
		} catch (Exception e) {
		}
	}

	private void paintConsumeRecordDetail(Graphics g, int relY) {
		int tempX = m_x + UITools.INITX_CONTENT;
		int tempY = m_x + UITools.INITY_CONTENT;
		tempX += UITools.IW_RECT_CONTENT;
		tempY += UITools.IH_RECT_CONTENT + relY;
		//
		String[] info = getDetail();
		if (info == null) {
			return;
		}
		try {
			g.setColor(COLOR_TITLE);
			g.drawString("消费项目:", tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_CONTENT);
			g.drawString(info[INDEX_CONSUME_NAME], tempX, tempY, 0);
			tempX += AbstractGameScreen.s_font.stringWidth(info[INDEX_CONSUME_NAME]) + 2;
			String x = "X";
			g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
			g.drawString(x, tempX, tempY, 0);
			tempX += AbstractGameScreen.s_font.stringWidth(x) + 1;
			g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_YESS));
			g.drawString(info[INDEX_CONSUME_NUM], tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			tempX = m_x + UITools.INITX_CONTENT;
			g.setColor(COLOR_TITLE);
			g.drawString("消费元宝:", tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_CONTENT);
			g.drawString(info[INDEX_CONSUME_SUM], tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_TITLE);
			g.drawString("消费时间:", tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
			g.setColor(COLOR_CONTENT);
			g.drawString(info[INDEX_CONSUME_TIME], tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
		} catch (Exception e) {
		}
	}

}
